package cz.myslivec.slovicka.action.lesson;

import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.FlushModeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;
import org.richfaces.model.ExtendedTableDataModel;
import org.richfaces.model.selection.Selection;
import org.richfaces.model.selection.SimpleSelection;
import org.richfaces.model.DataProvider;

import cz.myslivec.slovicka.dao.LessonDao;
import cz.myslivec.slovicka.entity.Lesson;

@Name("lessonsAction")
@Scope(ScopeType.CONVERSATION)
public class LessonsAction implements Serializable {
	private static final long serialVersionUID = -1025788776493355859L;

	@In
	EntityManager entityManager;

	@In
	LessonDao lessonDao;
	
	@Logger
	Log log;
	
	private Selection selection = new SimpleSelection();

	@Out(required = false, scope = ScopeType.SESSION)
	@In(required = false)
	Lesson selectedLesson;
	
	private List<Lesson> lessons;
	
	private boolean editMode = true;
	
	// INIT
	@Create
	public void init() {
		if(lessons == null) {
			lessons = lessonDao.list();
		}
	}

	// TABLE
	public ExtendedTableDataModel<Lesson> getModel() {

		return new ExtendedTableDataModel<Lesson>(
				new DataProvider<Lesson>() {
					private static final long serialVersionUID = 6724923130388579437L;

					public Lesson getItemByKey(Object arg0) {
						Lesson les = (Lesson) arg0;
						return lessonDao.get(les.getId());
					}

					public List<Lesson> getItemsByRange(int arg0, int arg1) {
						return lessons.subList(arg0, arg1);
					}

					public Object getKey(Lesson lesson) {
						return lesson;
					}

					public int getRowCount() {
						return lessons.size();
					}
				});
	}

	public Selection getSelection() {
		if(selectedLesson != null && selectedLesson.getId() != null) {
			if(!selection.isSelected(selectedLesson)) {
				((SimpleSelection) selection).clear();
				((SimpleSelection) selection).addKey(selectedLesson);
			}
		}
		return selection;
	}
	
	public void setSelection(Selection selection) {
		this.selection = selection;
		selectedLesson = null;
		
		if (selection != null && selection.getKeys().hasNext()) {
			Object o = selection.getKeys().next();

			if (o instanceof Long) {
				selectedLesson = lessonDao.get((Long) o);
			} else if (o instanceof Lesson) {
				selectedLesson = (Lesson) o;
			}
			
		}
	}

	// METHODS
	@Begin(flushMode = FlushModeType.MANUAL, join = true)
	public String preCreate() {
		selectedLesson = new Lesson();
		editMode = true;
		return "OK";
	}

	@Begin(flushMode = FlushModeType.MANUAL, join = true)
	public String preEdit() {
		//TODO ?
		//selectedLesson = lessonDao.get(selectedLesson.getId());
		editMode = true;
		log.info("<><><> {0}", selectedLesson.toString());
		
		return "OK";
	}
	
	// button actions
	@End(beforeRedirect = true)
	public String saveOrUpdate() {
		if(selectedLesson.getId() == null) {
			entityManager.persist(selectedLesson);
		}
		lessons = null;
		entityManager.flush();
		
		return "SAVED";
	}

	public String doOutlook() {
		// TODO
		return "OOO";
	}

	@Begin(flushMode = FlushModeType.MANUAL, join = true)
	public String doTest() {
		// TODO
		return "TTT";
	}
	
	@End(beforeRedirect = true)
	public String endTesting() {
		log.info("<> METHOD endTesting()");
		// TODO
		return "END_TEST";
	}
	
	public String doPreview() {
		// TODO
		return "PPP";
	}

	public String doDelete() {
		lessonDao.delete(selectedLesson);
		entityManager.flush();
		lessons = null;
		return "DELETED";
	}

	@End(beforeRedirect=true)
	public String endEdit() {
		return "OK";
	}
	
	//  BUTTON ENABLED METHODS
	public boolean isEditButtonEnabled() {
		if(selectedLesson != null) {
			return true;
		}
		return false;
	}

	public boolean isDeleteButtonEnabled() {
		if(selectedLesson != null) {
			return true;
		}
		return false;
	}
	
	public boolean isTestButtonEnabled() {
		if(selectedLesson != null) {
			return true;
		}
		return false;
	}

	public boolean isOutlookButtonEnabled() {
		if(selectedLesson != null) {
			return true;
		}
		return false;
	}
	
	// GETTERS & SETTERS
	public boolean isEditMode() {
		return editMode;
	}

	public void setEditMode(boolean editMode) {
		this.editMode = editMode;
	}

	public Lesson getSelectedLesson() {
		return selectedLesson;
	}

	public void setSelectedLesson(Lesson selectedLesson) {
		this.selectedLesson = selectedLesson;
	}

	public List<Lesson> getLessons() {
		return lessons;
	}

	public void setLessons(List<Lesson> lessons) {
		this.lessons = lessons;
	}

}
